<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>基础-axios</title>
		<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
		<script src="../../assets/js/base/vue.js" type="text/javascript" charset="utf-8"></script>
	</head>
	<body>
		<div id="app">
			<button @click="get">get请求</button>
			<button class="post">post请求</button>
			<ul>
				<li v-for="joke in jokes">{{joke}}</li>
			</ul>
			<div>
				<button @click="login"> T+city登陆</button>
			</div>
		</div>
		<script type="text/javascript">
			var vm = new Vue({
				el: '#app',
				data: {
					jokes: []
				},
				methods: {
					get: function() {
						axios.get("https://autumnfish.cn/api/joke/list?num=4").then(response => {
							this.jokes = response.data.jokes;
						})
					},
					login: function() {
						// header:api-version=v1
						// mobile:15221703574
						// password:user1234
						// 直接写｛｝对象不好使
						// 方法一new URLSearchParams();
						var params = new URLSearchParams();
						params.append('mobile', '15221703574');
						params.append('password', 'user1234');
						// 方法二：qs------失败，好像需要npm导包
						// var qs = require('qs');
						// qs.stringify({
						// 	mobile: '15221703574'
						// },{password:'user1234'})
						axios.post('http://192.168.1.197:8061/app/login', params, {
							headers: {
								'api-version': 'v1'
							}
						}).then(function(resp) {
							console.log(resp);
						}, function(error) {
							console.log(error);
						});
					}
				}
			})
			/*
				接口1:随机笑话
				请求地址:https://autumnfish.cn/api/joke/list
				请求方法:get
				请求参数:num(笑话条数,数字)
				响应内容:随机笑话
			*/
			document.getElementsByClassName("post")[0].onclick = function() {
				axios.post("https://autumnfish.cn/api/user/reg", {
					username: "jyx"
				}).then(function(response) {
					console.log(response);
				}, function(error) {
					console.log(error);
				})
			}
			/*
			     接口2:用户注册
			     请求地址:https://autumnfish.cn/api/user/reg
			     请求方法:post
			     请求参数:username(用户名,字符串)
			     响应内容:注册成功或失败
			 */
		</script>
	</body>
</html>
